package com.hc.util;

import java.awt.Color;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import com.hc.service.MessageService;




public class SystemConfigUtil {


	
	public static final String FILEPATH="file/systemConfig.properties";
	
	public static void readConfig(){
		
		
	File file = new File(FILEPATH);
		
		FileReader fileReader = null;

			try {
				fileReader = new  FileReader(file);
			} catch (FileNotFoundException e1) {
					e1.printStackTrace();
			}
			BufferedReader reader = new BufferedReader(fileReader);
			String linStr = "";
			

			
			try {
				while((linStr=reader.readLine())!=null){
					
				String[] results= 	linStr.split("=");
					
					if("mapFilePath".equals(results[0])){
						
						CommonData.mapFilePath = results[1];
					}
					else if("mapConfigFilePath".equals(results[0])){
						
						CommonData.mapConfigFilePath = results[1];
					}else if("mapscale".equals(results[0])){
						
						CommonData.mapscale = Integer.parseInt(results[1]);
					}else if("mapscaleUnit".equals(results[0])){
						
						CommonData.mapscaleUnit = Integer.parseInt(results[1]);
					}else if("lineWidth".equals(results[0])){
						CommonData.lineWidth = Integer.parseInt(results[1]);
			
					}else if("lineColor".equals(results[0])){
						String colorStr=results[1];
						String[] colorStrs= colorStr.split("_");
						CommonData.lineColor = new Color(Integer.parseInt(colorStrs[0]), Integer.parseInt(colorStrs[1]), Integer.parseInt(colorStrs[2]));
						
					}else if("isUseKalmanMath".equals(results[0])){
						CommonData.isUseKalmanMath = Boolean.parseBoolean(results[1]);
					}else if("KarmanMathUtilForX_Q".equals(results[0])){
						
						KarmanMathUtilForX.Q = Float.parseFloat(results[1]);
						
					}else if("KarmanMathUtilForX_R".equals(results[0])){
						
						KarmanMathUtilForX.R = Float.parseFloat(results[1]);
						
					}else if("KarmanMathUtilForY_Q".equals(results[0])){
						
						KarmanMathUtilForY.Q = Float.parseFloat(results[1]);
					}else if("KarmanMathUtilForY_R".equals(results[0])){
						
						KarmanMathUtilForY.R = Float.parseFloat(results[1]);
						
					}
//					else if("baseStation_tempNumber1".equals(results[0])){
//						MessageService.BASESTATIONTEMPNUMBER1 = Integer.parseInt(results[1]);
//						
//					}else if("baseStation_tempNumber2".equals(results[0])){
//						MessageService.BASESTATIONTEMPNUMBER2 = Integer.parseInt(results[1]);
//						
//					}else if("baseStation_tempNumber3".equals(results[0])){
//						MessageService.BASESTATIONTEMPNUMBER3 = Integer.parseInt(results[1]);
//						
//					}else if("baseStation_tempNumber4".equals(results[0])){
//						MessageService.BASESTATIONTEMPNUMBER4 = Integer.parseInt(results[1]);
//						
//					}
					else if("useMapConfigState".equals(results[0])){
						CommonData.useMapConfigState = Boolean.parseBoolean(results[1]);
						
					}else if("isUseBaseStation_FH_1".equals(results[0])){
						CommonData.isUseBaseStation_FH_1 = Boolean.parseBoolean(results[1]);
						
					}else if("isUseBaseStation_FH_2".equals(results[0])){
						CommonData.isUseBaseStation_FH_2 = Boolean.parseBoolean(results[1]);
						
					}else if("isUseBaseStation_FH_3".equals(results[0])){
						CommonData.isUseBaseStation_FH_3 = Boolean.parseBoolean(results[1]);
						
					}else if("isUseBaseStation_FH_4".equals(results[0])){
						CommonData.isUseBaseStation_FH_4 = Boolean.parseBoolean(results[1]);
						
					}else if("baseStation_FH_1".equals(results[0])){
						CommonData.baseStation_FH_1 =Integer.parseInt(results[1]);
						
					}else if("baseStation_FH_2".equals(results[0])){
						CommonData.baseStation_FH_2 =Integer.parseInt(results[1]);
						
					}else if("baseStation_FH_3".equals(results[0])){
						CommonData.baseStation_FH_3 =Integer.parseInt(results[1]);
						
					}else if("baseStation_FH_4".equals(results[0])){
						CommonData.baseStation_FH_4 =Integer.parseInt(results[1]);
						
					}else if("locationServer_ip".equals(results[0])){
						CommonData.LocationServer.LOCATIONSERVER_IP = results[1];
					}else if("locationServer_port".equals(results[0])){
						CommonData.LocationServer.LOCATIONSERVER_PORT = Integer.parseInt(results[1]);
					}
					
					
				}
				
				reader.close();
				fileReader.close();
			} catch (IOException e) {
					e.printStackTrace();
			}


	}
	
	public static void writeConfig(){
		StringBuilder sb = new StringBuilder();

			sb.append("mapFilePath=").append(CommonData.mapFilePath).append("\r\n");
			sb.append("mapConfigFilePath=").append(CommonData.mapConfigFilePath).append("\r\n");
			sb.append("mapscale=").append(CommonData.mapscale).append("\r\n");
			sb.append("mapscaleUnit=").append(CommonData.mapscaleUnit).append("\r\n");
			sb.append("lineWidth=").append(CommonData.lineWidth).append("\r\n");
			sb.append("lineColor=").append(CommonData.lineColor.getRed()+"_"+CommonData.lineColor.getGreen()+"_"+CommonData.lineColor.getBlue()).append("\r\n");
			sb.append("isUseKalmanMath=").append(CommonData.isUseKalmanMath).append("\r\n");
			sb.append("KarmanMathUtilForX_Q=").append(KarmanMathUtilForX.Q).append("\r\n");
			sb.append("KarmanMathUtilForX_R=").append(KarmanMathUtilForX.R).append("\r\n");
			sb.append("KarmanMathUtilForY_Q=").append(KarmanMathUtilForY.Q).append("\r\n");
			sb.append("KarmanMathUtilForY_R=").append(KarmanMathUtilForY.R).append("\r\n");
			sb.append("baseStation_tempNumber1=").append(MessageService.BASESTATIONTEMPNUMBER1).append("\r\n");
			sb.append("baseStation_tempNumber2=").append(MessageService.BASESTATIONTEMPNUMBER2).append("\r\n");
			sb.append("baseStation_tempNumber3=").append(MessageService.BASESTATIONTEMPNUMBER3).append("\r\n");
			sb.append("baseStation_tempNumber4=").append(MessageService.BASESTATIONTEMPNUMBER4).append("\r\n");
			sb.append("useMapConfigState=").append(CommonData.useMapConfigState ).append("\r\n");
			sb.append("isUseBaseStation_FH_1=").append(CommonData.isUseBaseStation_FH_1 ).append("\r\n");
			sb.append("isUseBaseStation_FH_2=").append(CommonData.isUseBaseStation_FH_2 ).append("\r\n");
			sb.append("isUseBaseStation_FH_3=").append(CommonData.isUseBaseStation_FH_3 ).append("\r\n");
			sb.append("isUseBaseStation_FH_4=").append(CommonData.isUseBaseStation_FH_4 ).append("\r\n");
			sb.append("baseStation_FH_1=").append(CommonData.baseStation_FH_1 ).append("\r\n");
			sb.append("baseStation_FH_2=").append(CommonData.baseStation_FH_2 ).append("\r\n");
			sb.append("baseStation_FH_3=").append(CommonData.baseStation_FH_3 ).append("\r\n");
			sb.append("baseStation_FH_4=").append(CommonData.baseStation_FH_4 ).append("\r\n");
			sb.append("locationServer_ip=").append(CommonData.LocationServer.LOCATIONSERVER_IP ).append("\r\n");
			sb.append("locationServer_port=").append(CommonData.LocationServer.LOCATIONSERVER_PORT ).append("\r\n");
			
			
		File file = new File(FILEPATH);
		FileWriter w;
		try {
			w = new FileWriter(file);
			w.write(sb.toString());
			w.flush();
			w.close();
		} catch (IOException e) {
			e.printStackTrace();
			
		}
	
	

	}
	public static void main(String[] args) {
	
		SystemConfigUtil.readConfig();
		SystemConfigUtil.writeConfig();
	
	}

}
